<?php 
	/**
	 * 检查故障原因
	 */

	include_once dirname(dirname(__FILE__))."/include/common.inc.php";
	include_once ROOT_PATH."/baihe/function.baihe.php";
	include_once ROOT_PATH."/zhenai/function.zhenai.php";
	include_once ROOT_PATH."/ganji/function.ganji.php";
	$global_site = "tools";
	
	$yesterday = date("Y-m-d", time()-60*60*24);
	
	//db
	printlog("准备检查数据表");
	$tblData = getTblName($yesterday);
	$tblArr = array("ganji_user", "ganji_city", "register_log", $tblData);
	foreach ($tblArr as $tbl){
		if (checkTblValid($tbl)) {
			printlog("$tbl ok ");
		}
		else{
			printlog("$tbl fail... ");
			exit;
		}
	}
	
	//adsl
	printlog("准备检查adsl自动拨号功能。。。");
	adsl_redial();
	$ip1 = getip();
	printlog("第一次外网ip为 $ip1 ");
	adsl_redial();
	$ip2 = getip();
	printlog("第二次外网ip为 $ip2 ");
	if (empty($ip2) || $ip1 == $ip2) {
		printlog("自动拨号功能有问题");
		exit;
	}
	else{
		printlog("自动拨号功能ok");
	}
	
	//smtp
	printlog("准备检查发送邮件功能");
	sendmailEx("测试邮件标题","测试邮件内容");
	printlog("已发送邮件，请查看");
	
	//register
	printlog("准备检查自动注册功能");
	printlog("准备注册珍爱网id");
	$new_zhenai_id = 0;
	for($i=1;$i<=10;$i++){
		printlog("第 $i 次注册尝试.....");
		$new_zhenai_id = registerZhenaiId();
		if ($new_zhenai_id) {
			printlog("注册成功，新注册id为 $new_zhenai_id ");
			break;
		}
		else{
			printlog("注册失败");
			adsl_redial();
		}
	}
	printlog("准备注册百合网id");
	$new_baihe_id = 0;
	for($i=1;$i<=10;$i++){
		printlog("第 $i 次注册尝试.....");
		$new_baihe_id = registerBaiheId();
		if ($new_baihe_id) {
			printlog("注册成功，新注册id为 $new_baihe_id ");
			break;
		}
		else{
			printlog("注册失败");
			adsl_redial();
		}
	}
	
	//crawl ： 百合和珍爱的测试数据均为1男1女，其中男的没照片，女的有照片，赶集的测试数据为一个搜索结果页
	//zhenai
	printlog("准备珍爱页面抓取");
	$zhenai_debug_uid_gg = 41270238; //心雨心
	$zhenai_debug_uid_mm = 41270292; //左微笑右眼泪
	@unlink("page_zhenai_gg.html");
	@unlink("page_zhenai_mm.html");
	$page = debug_zhenaiCrawl($zhenai_debug_uid_gg);
	file_put_contents("page_zhenai_gg.html", $page);
	printlog("页面保存至  page_zhenai_gg.html ");
	printlog("准备提取用户信息");
	debug_zhenaiExtract($page);
	$page = debug_zhenaiCrawl($zhenai_debug_uid_mm);
	file_put_contents("page_zhenai_mm.html", $page);
	printlog("页面保存至  page_zhenai_mm.html ");
	printlog("准备提取用户信息");
	debug_zhenaiExtract($page);
	
	printlog("准备百合页面抓取");
	$baihe_debug_uid_gg = 66787028; //头很痛的不二信
	$baihe_debug_uid_mm = 65362802; //MeLiSsa
	@unlink("page_baihe_gg.html");
	@unlink("page_baihe_mm.html");
	$page = debug_baiheCrawl($baihe_debug_uid_gg);
	file_put_contents("page_baihe_gg.html", $page);
	printlog("页面保存至 page_baihe_gg.html ");
	printlog("准备提取用户信息");
	debug_baiheExtract($page);
	$page = debug_baiheCrawl($baihe_debug_uid_mm);
	file_put_contents("page_baihe_mm.html", $page);
	printlog("页面保存至 page_baihe_mm.html ");
	printlog("准备提取用户信息");
	debug_baiheExtract($page);
	
	
	printlog("准备赶集页面抓取");
	@unlink("page_ganji.html");
	$url = "http://love.ganji.com/gz/mm/?&age_start=18&age_end=60&photo=0&order=3&list=2&page=2";
	printlog("待抓取的url : $url ");
	$opt_arr = array(CURLOPT_REFERER => "http://love.ganji.com/bj/mm/",		
					);
	$page = makeRequest($url, 
						"GET", 
						"", 
						false, 
						"", 
						"", 
						$opt_arr
						);
	file_put_contents("page_ganji.html", $page);
	printlog("页面保存至 page_ganji.html ");
	printlog("赶集数据提取的测试稍后再做");
	
	
	function checkTblValid($tblname)
	{
		global $conn;
		$sql = "select count(*) from $tblname ";
		$cnt = $conn->result($sql);
		return $cnt;
	}
	
	function debug_zhenaiCrawl($uid)
	{
		printlog("准备抓取珍爱页面 uid $uid ");
		$url = "http://album.zhenai.com/profile/getmemberdata.jsps?memberid=".$uid;
		printlog("待抓取的url : $url ");
		$page = makeRequest($url, 
							"GET" , 
							"", 
							false, 
							"", 
							ROOT_PATH."/zhenai/cookie/zhenai_request_cookie.txt",
							""
						);
		return $page;
	}
	
	function debug_baiheCrawl($uid)
	{
		printlog("准备抓取百合页面 uid $uid ");
		$url = "http://profile.baihe.com/new/BasicInfo.action?oppId=".$uid;
		printlog("待抓取的url : $url ");
		$page = makeRequest($url, 
							"GET", 
							"", 
							false, 
							"", 
							ROOT_PATH."/baihe/cookie/baihe_login_cookie.txt",
							"");
		return $page;
	}

	function debug_zhenaiExtract($page)
	{
		global $global_province_list;
		$page = iconv("GBK", 'UTF-8//IGNORE', $page);
			//tmp: 23岁，北京东城区，男士
		$tmp = simpleExtract('/<divclass=\"dataBox01\">(.*?)寻找/', $page, "tmp");//printlog($tmp);exit;
		$province = "";
		foreach ($global_province_list as $prov){
			if (strpos($tmp, $prov) !== false) {
				$province = $prov; //北京
				break;
			}
		}
		$gender = (strpos($tmp, "男士") !== false ) ? 0 : 1; //男为0 女为1
		$age = simpleExtract('/(\d\d)/', $tmp, "age"); //23
		$city = simpleExtract('/，(.*?)，/', $tmp, "tmp");
		$city = str_replace(array($province, "省"), "", $city); //东城区
		//printlog ("age: $age province: $province city: $city gender: $gender tmp: $tmp ");exit;
		$has_photo = simpleExtract('/<!--照片数：(\d+?)-->/', $page, "has_photo");
		$has_photo = ($has_photo)?1:0;
		printlog("提取结果： age:$age  city:$city  province:$province  gender:$gender  has_photo:$has_photo ");
	}
	
	function debug_baiheExtract($page)
	{
		global $global_province_list;
		$page = iconv("GBK", 'UTF-8//IGNORE', $page);
		$tmp = simpleExtract('/来自(.*?)的/', $page, "tmp");
		$province = "";
		foreach ($global_province_list as $prov){
			if (strpos($tmp, $prov) !== false) {
				$province = $prov;
				break;
			}
		}
		$age = simpleExtract('/(\d\d)/', $tmp, "age"); //20
		$city = str_replace(array($province, $age, "省", "岁"), "", $tmp); //济南
		//echo "age: $age province: $province city: $city tmp: $tmp ";exit;
		$gender = simpleExtract('/varoppGender=(\w+)/', $page, "gender");
		$gender = ($gender=="true")?0:1; //男为0 女为1
		$has_photo = simpleExtract('/oppMainPhoto=.*(nopic_.*)\./', $page, "has_photo");
		$has_photo = ($has_photo)?1:0;
	
		printlog("提取结果： age:$age  city:$city  province:$province  gender:$gender  has_photo:$has_photo ");
	}
	
?>